package com.qf.fmall2202.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.fmall2202.product.entity.ProductComments;
import com.qf.fmall2202.product.dao.ProductCommentsDao;
import com.qf.fmall2202.product.service.ProductCommentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.fmall2202.user.entity.User;
import com.qf.fmall2202.user.service.UsersService;
import com.qf.fmall2202.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品评价  服务实现类
 * </p>
 *
 * @author 徐英海
 * @since 2022-11-07
 */
@Service
public class ProductCommentsServiceImpl extends ServiceImpl<ProductCommentsDao, ProductComments> implements ProductCommentsService {
    @Autowired
    private UsersService usersService;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo queryProductCommentsService(String productId) {
        LambdaQueryWrapper<ProductComments> productCommentsLambdaQueryWrapper = new LambdaQueryWrapper<ProductComments>();
        productCommentsLambdaQueryWrapper.eq(ProductComments::getProductId, productId);
        List<ProductComments> productCommentsList = this.list(productCommentsLambdaQueryWrapper);
        Map<Integer, List<ProductComments>> totalMap = productCommentsList.stream()
                .collect(Collectors.groupingBy(ProductComments::getCommType));
        BigDecimal total = BigDecimal.valueOf(productCommentsList.size());
        BigDecimal badTotal = BigDecimal.valueOf(totalMap.get(-1) == null ? 0 : totalMap.get(-1).size());
        BigDecimal midTotal = BigDecimal.valueOf(totalMap.get(0) == null ? 0 : totalMap.get(0).size());
        BigDecimal goodTotal = BigDecimal.valueOf(totalMap.get(1) == null ? 0 : totalMap.get(1).size());
        BigDecimal percent = null;
        try {
            percent = goodTotal.multiply(BigDecimal.valueOf(100)).divide(total, 2, RoundingMode.HALF_UP);
        } catch (ArithmeticException e) {
            percent = BigDecimal.valueOf(0);
        }
        Map<String, BigDecimal> map = new HashMap<>();
        map.put("total", total);
        map.put("badTotal", badTotal);
        map.put("midTotal", midTotal);
        map.put("goodTotal", goodTotal);
        map.put("percent", percent);
        return ResultVo.ok("查询成功", map);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo queryDetailCommonts(String productId, Integer pageNum, Integer limit) {
        Page<ProductComments> page = new Page<>(pageNum, limit);
        this.page(page, new LambdaQueryWrapper<ProductComments>().eq(ProductComments::getProductId, productId));
        List<ProductComments> productCommentsList = page.getRecords();
        List<String> productCommentsUserIdList = productCommentsList.stream().map(ProductComments::getUserId).collect(Collectors.toList());
        List<User> userList = null;
        try {
            userList = usersService.list(new LambdaQueryWrapper<User>().in(User::getUserId, productCommentsUserIdList));
        } catch (BadSqlGrammarException e) {
            userList = new ArrayList<>();
        }
        Map<Integer, User> userMap = userList.stream().collect(Collectors.toMap(User::getUserId, user -> user));
        productCommentsList.forEach(
                productComments -> {
                    User user = userMap.get(Integer.parseInt(productComments.getUserId()));
                    productComments.setUsername(user.getUsername());
                    productComments.setNickname(user.getNickname());
                    productComments.setUserImg(user.getUserImg());
                }
        );
        Map<String, Object> map = new HashMap<>();
        map.put("pageCount", page.getPages());
        map.put("count", page.getTotal());
        map.put("list", productCommentsList);
        return ResultVo.ok("查询成功", map);
    }
}
